import React from 'react';
import routes from "./router";
import { Route, Switch, Redirect} from 'react-router-dom';
// 引入包管理工具
// import { renderRoutes, RouteConfig } from "react-router-config";
import './App.less';
import Login from "@v/login/login"
import {getStore} from "@/plugins/cache";


function App() {

    // @ts-ignore
    const PrivateRoute = ({component: Component, ...rest}) => {
        const isLoggedIn = Boolean(getStore({name: "token"}))
        // console.log(isLoggedIn)
        return (
            <Route
                {...rest}
                render={props =>
                    isLoggedIn ? (
                        <Component {...props} />
                    ) : (
                        <Redirect to="/login"/>
                    )
                }
            />
        );
    }

    const routerList = routes.filter((item: any) => item.exact);
    // console.log(routerList)

    return (
        <div className="App">

                <Switch>
                    <Route path="/login" key="login" component={Login}/>
                    {
                        routerList.map((item: any) => {
                            return (
                                <PrivateRoute key={item.path} path={item.path} exact={item.exact} component={item.component}/>
                            )
                        })
                    }
                    <Redirect to="/login"/>
                </Switch>

            {/*{renderRoutes(routes as RouteConfig[])}*/}
        </div>
    );
}

export default App;
